package es.cazadoresmadridejos.beans.dao.reason.impl;

import java.sql.SQLException;
import java.util.List;

import org.displaytag.pagination.PaginatedList;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import es.cazadoresmadridejos.beans.dao.reason.ReasonDao;
import es.cazadoresmadridejos.common.bean.dao.impl.DaoImpl;
import es.cazadoresmadridejos.model.Reason;
import es.cazadoresmadridejos.objs.web.Page;
import es.cazadoresmadridejos.objs.web.PaginatedListImpl;

@SuppressWarnings("unchecked")
@Repository("reasonDao")
public class ReasonDaoImpl extends DaoImpl<Reason> implements ReasonDao{

	@Override 
	public Reason newInstance() {
		return new Reason();
	}

	@Override
	public Reason getByReason(final Long reasonFromDate) {
		return (Reason) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session) throws HibernateException,
			SQLException {

				StringBuilder hql = new StringBuilder();
				hql.append("select r ")
				.append(" from Reason r ")
				.append(" where r.year = :year")
				.append("");

				Query query = session.createQuery(hql.toString());
				query.setLong("year", reasonFromDate);

				return query.uniqueResult();
			}
		});
	}
	@Override
	public List<Reason> findAll() {
		return (List<Reason>) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session) throws HibernateException,
			SQLException {
				StringBuilder hql = new StringBuilder();
				hql.append("select t ");
				hql.append(" from Reason t ");
				hql.append(" order by t.year desc ");

				Query query = session.createQuery(hql.toString());

				return query.list();
			}
		});
	}

	@Override
	public PaginatedList findAllPaginated(final Page page) {
		return (PaginatedList) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
				.append("select r ")
				.append(" from Reason r ");

				StringBuilder hqlCount = new StringBuilder();
				hqlCount
				.append("select count(r) ")
				.append(" from Reason r ");

				hql.append(getSort(page,"r"));

				Query query = session.createQuery(hql.toString());
				Query queryCount = session.createQuery(hqlCount.toString());

				query.setMaxResults(page.getLimit());
				query.setFirstResult(page.getLimit() * (page.getPage() - 1));

				return new PaginatedListImpl<Object>(query.list(), 
						page, 
						((Long)queryCount.uniqueResult()).intValue());
			}
		});
	}

	@Override
	public List<Reason> findAllNotAssociated(final Long budgetId) {
		return (List<Reason>) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
				.append("select r ")
				.append(" from Reason r ")
				.append(" where r.id not in (select b.reason.id from Budget b ");
				if(budgetId != null){
					hql.append("where b.id != :budgetId)");
				}
				hql.append(")");

				Query query = session.createQuery(hql.toString());

				if(budgetId != null){
					query.setLong("budgetId", budgetId);
				}
				return query.list();
			}
		});
	}
}
